<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

class m_medias_actions_block_ListAction extends m_backoffice_extends_Action
{
    private $nbDocTot;
    
	public function execute()
	{
		$docId = $this->getRequestParameter("docId");
		$docLang = $this->getRequestParameter("docLang");
		$tabName = $this->getRequestParameter("tabName");
		$isTabbed = $this->getRequestParameter("tabbed");
		$isRoot = false;

		if(!is_null($docId) && !is_null($docLang))
		{
			$folder = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));
		}
		else
		{
			$folder = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode("medias");
			$isRoot = true;
		}
        
		if(!$isRoot)
		{
            $nbMedias = $folder->getMediaNb();

			if($tabName == "files" || $tabName == "")
			{
                $listService = m_backoffice_services_ListService::getInstance();
                
                //Sort and search parameters
                $sortParams = $listService->getSortParams("medias", $this->getRequestParameter("column"), $this->getRequestParameter("order"));
                $searchParams = $listService->getSearchParams("medias", $this->getRequestParameter("searchValues"));

                //Get the nbDocTot;
                $nbDocTot = $listService->getDocuments($folder, array("modules_medias/media"), null, null, array(), $searchParams, true);
                $pagination = $listService->pagination("medias", $nbDocTot, $this->getRequestParameter("numPage"), $this->getRequestParameter("nbDoc"), 10);

                $medias = $listService->getDocuments($folder, array("modules_medias/media"), $pagination["nbDoc"], $pagination["numPage"], $sortParams, $searchParams);
                $this->addToModel("medias", $medias);
			}
			
			if($tabName == "parameters")
			{
                $paramService = m_medias_services_ParameterService::getInstance();
				$parameters = $paramService->getParametersByFolderId($folder->getUniqueId(), $folder->getLang());
				$this->addToModel("newParamUrl", f_core_Routing::getUrl("mediaParameter", array("folderLang" => strtolower($folder->getLang()), "folderId" => $folder->getUniqueId())));
                $this->addToModel("parameters", $parameters);
                $this->addToModel("cropLabels", $paramService->getResizeModeLabel());
			}
			

            $fileLink = f_core_Routing::getUrl("mediasListTab", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang()), "tabName" => "files"));
            $this->addToModel("fileLink", $fileLink);
            
			if($isTabbed == "false")
			{
				$paramsLink = f_core_Routing::getUrl("mediasListTab", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang()), "tabName" => "parameters"));
				$this->addToModel("paramsLink", $paramsLink);
				$propertiesLink = f_core_Routing::getUrl("mediasListTab", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang()), "tabName" => "properties"));				
				$this->addToModel("propertiesLink", $propertiesLink);
			}

            $this->addToModel("nbMedias", $nbMedias);
            $this->addToModel("docSearchParams", $searchParams);
            $this->addToModel("searchParams", $this->getContext("searchParams_medias"));
            $this->addToModel("sortParams", $sortParams);
            $this->addToModel("pagination", $pagination);
		}
		else
		{
			if($folder->getFolderNb() > 0)
			{
                $relManager = f_relation_Manager::getInstance();
                $subFolders = $relManager->getChildsByDocTypeRecursive($folder->getUniqueId(), array('modules_backoffice/Folder'));
				$this->addToModel("subFolders", $subFolders);
			}
			
			$fileLink = f_core_Routing::getUrl("mediasRoot");
			$this->addToModel("fileLink", $fileLink);
			$paramsLink = f_core_Routing::getUrl("mediasListTab", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang()), "tabName" => "parameters"));
            $this->addToModel("paramsLink", $paramsLink);
			$addFolderLink = f_core_Routing::getUrl("addMediaFolder", array("docId" => $folder->getUniqueId(), "docLang" => strtolower($folder->getLang())));
			$this->addToModel("addFolder", $addFolderLink);
		}
		
		$this->addToModel("folder", $folder);
		$this->addToModel("isTabbed", $isTabbed);
		$this->addToModel("tabName", $tabName);
        		
		if(!$isRoot)
		{            
			$this->setView('success');
		}
		else
		{
			$this->setView('rootSuccess');
		}
	}	
}